<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('�޸ķ�����Ϣ����')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: select2-css" />
	<th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-info-edit" th:object="${sysAppPrescriptionSort}">
            <input name="id" th:field="*{id}" type="hidden">
            <div class="form-group">    
                <label class="col-sm-3 control-label">分类名称：</label>
                <div class="col-sm-8">
                    <input disabled="true" name="sortName" th:field="*{sortName}" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">对应方剂</label>
                <div class="col-sm-8">
	                <select name="prescriptioninfos" id="prescriptioninfos" class="form-control select2-multiple" th:with="type=${@prescription.getInfos(1)}" multiple>
	                   <option th:each="prescription : ${type}" th:text="${prescription.title}" th:value="${prescription.id}"></option>
	                </select>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: select2-js" />
    <th:block th:include="include :: bootstrap-select-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/sort";
        $("#form-info-edit").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/edit1", $('#form-info-edit').serialize());
            }
        }

        $("input[name='createDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(function() {
            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    height : 260,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
							var obj = this;
							var data = new FormData();
							data.append("file", files[0]);
							$.ajax({
								type: "post",
								url: ctx + "common/upload",
								data: data,
								cache: false,
								contentType: false,
								processData: false,
								dataType: 'json',
								success: function(result) {
									if (result.code == web_status.SUCCESS) {
										$('#' + obj.id).summernote('insertImage', result.url);
									} else {
										$.modal.alertError(result.msg);
									}
								},
								error: function(error) {
									$.modal.alertWarning("上传失败");
								}
							});
						}
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            })
            
            var preId = $("#id").val();
            var arr = []
            	$.ajax({
    				type: "post",
    				url: ctx + "system/info" + "/selectInfoBySortId",
    				data: {
    					sortId:preId
    				},
    				dataType: 'json',
    				success: function(result) {
    					if (result.code == web_status.SUCCESS) {
    						for(let i in result.rows){
    						    console.log(result.rows[i].id)
    						    arr.push(result.rows[i].id)
    						}
    						$("#prescriptioninfos").val(arr).select2()
    					} else {
    						$.modal.alertWarning("默认加载失败");
    					}
    				},
    				error: function(error) {
    					$.modal.alertWarning("默认加载失败");
    				}
    			});
            /* 存储方剂和方剂分类的关联关系 */
            $('#prescriptioninfos').on('select2:select', function (e) {
            	var preId = $("#id").val();
            	$.ajax({
    				type: "post",
    				url: ctx + "system/infosort" + "/saveSort",
    				data: {
    					infoId:e.params.data.id,
    					sortId:preId
    				},
    				dataType: 'json',
    				success: function(result) {
    					if (result.code == web_status.SUCCESS) {
    						$.modal.alertWarning("设置成功");
    					} else {
    						$.modal.alertWarning("设置失败，请查看错误信息");
    					}
    				},
    				error: function(error) {
    					$.modal.alertWarning("设置失败，请查看错误信息");
    				}
    			});
        	});
            
        	/* 删除时调用 */
        	$('#prescriptioninfos').on('select2:unselect', function (e) {
        		var preId = $("#id").val();
            	$.ajax({
    				type: "post",
    				url: ctx + "system/infosort" + "/deleteByInfoIdAndSortId",
    				data: {
    					infoId:e.params.data.id,
    					sortId:preId
    				},
    				dataType: 'json',
    				success: function(result) {
    					if (result.code == web_status.SUCCESS) {
    						$.modal.alertWarning("删除成功");
    					} else {
    						$.modal.alertWarning("删除失败");
    					}
    				},
    				error: function(error) {
    					$.modal.alertWarning("删除失败");
    				}
    			});
        	});
            
            
        });
        
    </script>
</body>
</html>